package com.groupThree.wx.controller;

import com.groupThree.bean.BootStrapTableExpress;
import com.groupThree.bean.Express;
import com.groupThree.bean.Message;
import com.groupThree.bean.User;
import com.groupThree.mvc.ResponseBody;
import com.groupThree.mvc.ResponseView;
import com.groupThree.service.ExpressService;
import com.groupThree.util.DateFormatUtil;
import com.groupThree.util.JSONUtil;
import com.groupThree.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class QRCodeController {

    @ResponseView("/wx/createQRCode.do")
    public String createQRcode(HttpServletRequest request, HttpServletResponse response){
        String code = request.getParameter("code");
        String type = request.getParameter("type");
        String userPhone = null;
        String qrCodeContent = null;

        if ("express".equals(type)){
            //展示单个快递信息
            qrCodeContent = "express_" + code;

        }else {
            //展示所有的快速
            User wxUser = UserUtil.getWxUser(request.getSession());
            userPhone = wxUser.getU_phone();
            qrCodeContent = "userPhone_" + userPhone;



        }
        HttpSession session = request.getSession();
        session.setAttribute("qrcode",qrCodeContent);
        return "/personQRcode.html";

    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRcode(HttpServletRequest request, HttpServletResponse response){

        HttpSession session = request.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        Message message = new Message();
        if (qrcode == null){
            message.setStatus(-1);
            message.setResult("取件码出错，请重新扫码");
        }else {
            message.setStatus(0);
            message.setResult(qrcode);

        }

        String json = JSONUtil.toJSON(message);
        return json;
    }
    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest request,HttpServletResponse response){
        String code = request.getParameter("code");
        boolean flag = ExpressService.updateStatus(code);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("取件成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("取件码不存在,请用户更新二维码");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest request,HttpServletResponse response){
        String code = request.getParameter("code");
        Express e = ExpressService.findByCode(code);
        BootStrapTableExpress e2 =null;
        if(e!=null){
            e2 = new BootStrapTableExpress(e.getId(), e.getNumber(), e.getUsername()
                    , e.getUserPhone(), e.getCompany(), e.getCode(),
                    DateFormatUtil.format(e.getInTime()), e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime()),e.getStatus()==0?"待取件":"已取件", e.getSysPhone());
        }
        Message msg = new Message();
        if(e == null){
            msg.setStatus(-1);
            msg.setResult("取件码不存在");
        }else{
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(e2);
        }
        return JSONUtil.toJSON(msg);
    }

}
